package cn.edu.henu.team.ui;

import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.article_brief;
import cn.edu.henu.team.bean.drugList;
import cn.edu.henu.team.utils.PicturePreviewActivity;

public class DrugActivity extends BaseActivity {
   private String objectId;
   private ImageView back;
   private ImageView dimage;
   private String dimage_url;
   private TextView drugname;
   private TextView disaster;
   private TextView function;
   private SmartRefreshLayout refreshSrl;
   @Override
   protected int initLayout() {
      return R.layout.activity_drug;
   }

   @Override
   protected void initView() {
      Intent it = getIntent();
      objectId = it.getStringExtra("objectId");

      back = findViewById(R.id.dback);
      back.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            finish();
            back.setBackgroundColor(Color.parseColor("#666699"));
         }
      });
      drugname = findViewById(R.id.drugName);
      dimage = findViewById(R.id.drugimage);
      disaster = findViewById(R.id.disaster);
      function = findViewById(R.id.function);
      refreshSrl = findViewById(R.id.refresh_srl_detial);
      dimage.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent it = new Intent(DrugActivity.this, PicturePreviewActivity.class);
            it.putExtra("url", dimage_url);
            startActivity(it);
         }
      });
      //刷新
      refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
      refreshSrl.setRefreshFooter(new ClassicsFooter(getApplicationContext()));


      refreshSrl.setOnRefreshListener(new OnRefreshListener() {
         @Override
         public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            refreshSrl.finishRefresh(1000/*,false*/);
            initData();
         }
      });
      refreshSrl.setOnLoadMoreListener(new OnLoadMoreListener() {
         @Override
         public void onLoadMore(RefreshLayout refreshlayout) {
            refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
            initData();
         }
      });

   }

   @Override
   protected void initData() {
      BmobQuery<drugList> query = new BmobQuery("drugList");
      query.getObject(objectId, new QueryListener<drugList>() {
         @Override
         public void done(drugList object, BmobException e) {
            if (e == null) {
               drugname.setText(object.getDrug());
               dimage_url = object.getImage();
               RequestOptions requestOptions = new RequestOptions()
                       .placeholder(R.drawable.drugloading)
                       .error(R.drawable.drugloading)
                       .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
               Glide.with(getApplicationContext()).load(dimage_url).apply(requestOptions).into(dimage);
               disaster.setText(object.getKind());
               function.setText(object.getInfo());

            } else {
               Toast.makeText(getApplicationContext(), "药品查询失败", Toast.LENGTH_SHORT).show();
            }
         }
      });

   }
}
